package com.jlkf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jlkf.ys.qiniu.StringUtils;
import com.jlkf.ys.qiniu.UrlSafeBase64;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;
import com.platform.server.util.WxpayDemoUtils;
import com.qiniu.processing.OperationManager;
import com.qiniu.processing.OperationStatus;

@Controller
@RequestMapping("/qiniu")
public class TranscodingController {
	
	@ResponseBody
	@RequestMapping(value = "/transcoding",method = RequestMethod.GET)
	public String transcoding(String key,String type) throws Exception{
		String accessKey = "CYA2nd06bG-3ge6106lJcNC9mugAXqOXf1CW3-Qo";
		String secretKey = "WwZWgqVgOlVHF7PlZHNFQ7eWs28QgQxZOaYUXx6a";
		//待处理文件所在空间
		String bucket = "hqsmapp";
		//String bucket = "hqsmapp&key=o_1ccuothlm8701tjqmkfkpq1982a.mp4&fops=avthumb%2Fflv%2Fr%2F30%2Fvb%2F256k%2Fvcodec%2Flibx264%2Far%2F22050%2Fab%2F64k%2Facodec%2Flibmp3lame&notifyURL=http%3A%2F%2Ffake.com%2Fqiniu%2Fnotify";
		//待处理文件名
		com.qiniu.util.Auth auth = com.qiniu.util.Auth.create(accessKey, secretKey);
		//数据处理指令，支持多个指令
		String orderid = WxpayDemoUtils.getRandomString(32);
		orderid = orderid.substring(16,orderid.length()) + ".mp4";
		String saveMp4Entry = String.format("%s:" + orderid, bucket);
		//String saveJpgEntry = String.format("%s:vframe_test_target.jpg", bucket);
		//String avthumbMp4Fop = String.format("avthumb/mp4|saveas/%s", UrlSafeBase64.encodeToString(saveMp4Entry));
		String avthumbMp4Fop = "";
		if("1".equals(type)){	//高清
			avthumbMp4Fop = String.format("avthumb/mp4/s/1280x720/vb/1.25m|saveas/%s", UrlSafeBase64.encodeToString(saveMp4Entry));
		}else if("2".equals(type)){		//标清
			avthumbMp4Fop = String.format("avthumb/mp4/s/848x480/vb/1.25m|saveas/%s", UrlSafeBase64.encodeToString(saveMp4Entry));
		}else if("3".equals(type)){
			avthumbMp4Fop = String.format("avthumb/mp4/s/1920x1080/vb/1.25m|saveas/%s", UrlSafeBase64.encodeToString(saveMp4Entry));
		}
		//String vframeJpgFop = String.format("vframe/jpg/offset/1|saveas/%s", UrlSafeBase64.encodeToString(saveJpgEntry));
		//将多个数据处理指令拼接起来
		String persistentOpfs = StringUtils.join(new String[]{avthumbMp4Fop}, ";");
		//数据处理队列名称，必须
		String persistentPipeline = "hqsmdl";
		//数据处理完成结果通知地址
		String persistentNotifyUrl = "http://120.79.157.154:8899/hqsm-oa/qiniu/notify/";
		//构造一个带指定Zone对象的配置类
		com.qiniu.storage.Configuration cfg = new com.qiniu.storage.Configuration(com.qiniu.common.Zone.zone0());
		//...其他参数参考类注释
		//构建持久化数据处理对象
		OperationManager operationManager = new OperationManager(auth, cfg);
	    String persistentId = operationManager.pfop(bucket, key, persistentOpfs, persistentPipeline, persistentNotifyUrl, true);
	    //可以根据该 persistentId 查询任务处理进度
	    System.out.println(persistentId);
	    OperationStatus operationStatus = operationManager.prefop(persistentId);
	    //解析 operationStatus 的结果
	    return "http://bbs.shituzx.com/" + orderid;
	}
	
	@ResponseBody
	@RequestMapping(value = "/notify",method = RequestMethod.POST)
	public void notufy(){
		System.out.println("回调成功");
	}
	
}
